import {
	HTTP_REQUEST_URL,
	HEADER,
	TOKENNAME
} from '@/config/app';


/**
 * 发送请求
 */
function baseRequest(url, method, data, ) {
	let Url = HTTP_REQUEST_URL,
		header = HEADER;
	let token = uni.getStorageSync('token')	
		if (token) header[TOKENNAME] ='Bearer ' + token
	return new Promise((reslove, reject) => {
		uni.showLoading({
			title: "加载中"
		})
		uni.request({
			url: Url + '/api/' + url,
			method: method || 'GET',
			header: header,
			data: data || {},
			success: (res) => {
				uni.hideLoading()
				console.log('请求状态码',res.data.status)
				if(res.data.status == 410000){
					uni.showToast({
						title:'登录状态失效!',
						icon:'none'
					})
					uni.navigateTo({
						url:'/pages/login/index'
					})
				}
				reslove(res.data)
				// console.log(res)
				//  if (res.data.status == 200)
				// 	reslove(res.data, res);
				// else if ([410000, 410001, 410002].indexOf(res.data.status) !== -1) {
				// 	// toLogin();
				// 	reject(res.data);
				// } else
				// 	reject(res.data.msg || '系统错误');
			},
			fail: (msg) => {
				uni.hideLoading()
				reject('请求失败');
			}
		})
	});
}

const request = {};

['options', 'get', 'post', 'put', 'head', 'delete', 'trace', 'connect'].forEach((method) => {
	request[method] = (api, data, opt) => baseRequest(api, method, data, opt || {})
});



export default request;
